var fs = require('fs');

var myArgs = process.argv.slice(1);

var input = [], vCardInFile = [], vCardsNope = [];

var i=0,nbvCards=0,recup;
var vCardplit, v=0,vCardSimilaire = [];
var vCardsIdentiques,isIdentiques=false;

var names, namesRecup, namesArranged;
var phone, phoneRecup, phoneArranged;
var mail, mailRecup, mailArranged;

var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function vCard(name,mail,phone) {
	this.name = name;
	this.mail = mail;
	this.phone = phone;
	
	show:{
		console.log("\n"+this.name+"\n"+this.phone+"\n"+this.mail);
	}
};

fs.readFile(myArgs[1], 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  
  input = data.split(/\s/);
  while(true)
  {
  getvCardData();
  }			
  
});

function next(){
	var curS = input.shift();
	return curS
}

function getvCardData()
{
 recup = next();
 if (recup != "END:VCARD")
 {
	if (recup.substring(0,2)=="N:")
	{
	namesWithDots = recup.substring(2,recup.length-1);
	namesWithoutDots = namesWithDots.split(";");
	namesArranged = namesWithoutDots[1]+" "+namesWithoutDots[0];
	}
	if (recup.substring(0,4)=="TEL;")
	{
	phone = recup.substring(4,recup.length);
	phoneRecup = phone.split(":");
	phoneArranged = phoneRecup[phoneRecup.length-1];
	}
	if (recup.substring(0,5)=="EMAIL")
	{
	mail = recup.substring(6,recup.length);
	mailRecup = mail.split(":");
	mailArranged = mailRecup[mailRecup.length-1];
	}
 } else {
 
 /*on arrive à la fin d'une vCard
 on va sauvegarder notre vCard dans une arraylist de vCards afin de pouvir y retourner plus tard
 */
 vCard1 = new vCard(namesArranged,mailArranged,phoneArranged);
 vCard1.show;
 vCardInFile[nbvCards] = vCard1; 
 nbvCards ++;
 v=0;
 vCardSimilaire = [];
	 if (recup != "BEGIN:VCARD")
	 {
		 recup = next();
		 v++
		 //si on atteint la fin du fichier vcard.vcf
			 if (v > 2)
			 {
				console.log("\n"+nbvCards+" vCard(s)");
				for (var j=0;j<nbvCards;j++)
				{
					if (!vCardsNope[j])
						{
					vCardsIdentiques,isIdentiques=false;
					for (var k=nbvCards;k>j;k--)
					{	
						if ((vCardInFile[k-1].name)==(vCardInFile[j].name)
								&& ((k-1)!=(j)))
						{
							if (!isIdentiques)
							{
								isIdentiques = true;
								vCardsIdentiques = (";"+k);
									
							} else {
								vCardsIdentiques = vCardsIdentiques.concat((";"+k).toString());
							}
							vCardsNope[k-1]=true;;
						}
					}
					if (isIdentiques)
					{
						vCardsIdentiques = (j+1).toString().concat(vCardsIdentiques);
						console.log(vCardsIdentiques);
						vCardSimilaire.push(vCardsIdentiques);
					}
					}
				}
				console.log(vCardSimilaire);
				for (j=0;j<vCardSimilaire.length;j++)
				{
					vCardplit = vCardSimilaire[j].split(";");
					console.log("\n"+vCardSimilaire[j]);
					for (k=0;k<vCardplit.length;k++)
					{
						console.log((vCardInFile[vCardplit[k]-1]));
					}
				}
					/*rl.question("Quel vCard voulez-vous conserver ? ", function(answer) {
					switch (answer) {

					case "exit":
					setTimeout(rl.close(), 1000);
					break;

					case "1":
					break;


					default:
					console.log("commande non valide");
					setTimeout(rl.close(), 1000);
					}
					});*/
				process.exit(0);
			 }
	 }
 }
 
}

function blankSpace()
{

}



